home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
TPUG - Toronto PET Users Group
/
TPUG Users Group CD
/
TPUG Users Group CD.iso
/
AMIGA
/
AMICUS
/
AMICUS01.ADF
/
C
/
objfix.c
< prev
next >
Wrap
C/C++ Source or Header
|
1985-11-15
|
12KB
|
341 lines
/* objfix.c */
/****************************************************************/
/* Fixup routine for object files output from Lattice C */
/* makes external symbols available to Wack! */
/* */
/* part of the Amiga Programmer's Library */
/* Note, originally compiled with microsoft C 3.0 for the PC */
/* */
/* Copyright (c) 1985 by Michael G. Lehman, MaxiCorp */
/* All rights reserved, posted on bulletin boards by */
/* permission. */
/****************************************************************/
/* define the right symbols for your C compiler and environment */
#define LATTI
#undef MSC30
#include <stdio.h>
#include <fcntl.h>
/* these are needed for Microsoft C v3.0 */
#ifdef MSC30
#include <sys\types.h> /*included so sys\stat.h works */
#include <sys\stat.h> /* included to get S_IREAD && S_IWRITE */
#define O_RAW O_BINARY
#endif
struct {
unsigned long len; /* symbol table field length */
unsigned long sym[40]; /* 160 chars max */
unsigned long value;
} symhunk[100]; /* allow for 100 symbols */
int nxtsym;
int width;
long startoffset;
int f;
main(argc,argv)
int argc;
char **argv;
{
int infile;
long l;
unsigned long i,j;
int open();
char *fname;
unsigned long lbuf;
int debug;
unsigned char typ;
if (argc < 2)
{
printf("Usage: objfix <cnvfile>\n");
exit(0);
}
debug = (argc > 2);
fname = argv[1];
if (!*fname)
return(0);
infile = open(fname,0);
if (infile == -1)
{
printf("\n\nUnable to open \"%s\" for dumping\n",fname);
exit(10);
}
#ifdef MSC30
f = creat("cnv.tmp",S_IREAD | S_IWRITE); /* assume binary mode */
#endif
#ifdef LATTI
f = creat("cnv.tmp", O_RAW | O_RDWR ); /* assume binary mode */
#endif
printf("\nConverting file: \"%s\" \n\n",fname);
nxtsym = 0;
while(1)
{
l = read(infile,&lbuf,4);
if (l!=4)
break;
if (lbuf != 0xF2030000)
putit(lbuf);
normalize(&lbuf);
if (debug)
printf("Block type: 0x%x\n",lbuf);
switch(lbuf)
{
case 0x3E7: /* hunk_unit */
case 0x3E8: /* hunk_name */
case 0x3E9: /* hunk_code */
case 0x3EA: /* hunk_data */
read(infile,&lbuf,4);
putit(lbuf);
normalize(&lbuf);
for(i=0; i < lbuf; i++)
{
read(infile,&j,4);
putit(j);
}
break;
case 0x3EB: /* hunk_bss */
read(infile,&lbuf,4);
putit(lbuf);
break;
case 0x3EC: /* hunk_r32 */
case 0x3ED: /* hunk_r16 */
case 0x3EE: /* hunk_r8 */
read(infile,&lbuf,4);
putit(lbuf);
read(infile,&j,4); /* hunk_number */
putit(j);
normalize(&lbuf);
while(lbuf)
{
for(i=0; i < lbuf; i++)
{
read(infile,&j,4);
putit(j);
}
read(infile,&lbuf,4);
putit(lbuf);
normalize(&lbuf);
if (lbuf)
{
read(infile,&j,4); /* hunk_number again */
putit(j);
}
}
break;
case 0x3EF: /* hunk_ext */
read(infile,&lbuf,4);
putit(lbuf);
j = lbuf;
normalize(&lbuf);
while (lbuf)
{
typ = (lbuf >> 24) & 0xFF;
lbuf &= 0xFFFFFF;
if (debug)
printf(" hunk_ext, typ=%d, len=%d\n",typ,lbuf);
switch(typ)
{
case 1: /* ext_def */
symhunk[nxtsym].len = j & 0xFFFFFF00;
for(i=0; i < lbuf; i++)
{
read(infile,&j,4);
putit(j);
symhunk[nxtsym].sym[i] = j;
}
read(infile,&j,4); /* value */
putit(j);
symhunk[nxtsym].value = j;
nxtsym++;
break;
case 2: /* ext_abs */
case 3: /* ext_res */
for(i=0; i < lbuf; i++)
{
read(infile,&j,4);
putit(j);
}
read(infile,&j,4); /* value */
putit(j);
break;
case 129: /* ext_ref32 */
case 131: /* ext_ref16 */
case 132: /* ext_ref8 */
for(i=0; i < lbuf; i++)
{
read(infile,&j,4);
putit(j);
}
read(infile,&lbuf,4); /* value */
putit(lbuf);
normalize(&lbuf);
for(i=0; i < lbuf; i++)
{
read(infile,&j,4);
putit(j);
}
break;
case 130: /* ext_common */
for(i=0; i < lbuf; i++)
{
read(infile,&j,4);
putit(j);
}
read(infile,&j,4); /* size */
putit(j);
read(infile,&lbuf,4);
putit(lbuf);
normalize(&lbuf);
for(i=0; i < lbuf; i++)
{
read(infile,&j,4);
putit(j);
}
break;
}
read(infile,&lbuf,4);
putit(lbuf);
j = lbuf;
normalize(&lbuf);
}
break;
case 0x3F0: /* hunk_symbol */
read(infile,&lbuf,4);
putit(lbuf);
j = lbuf;
normalize(&lbuf);
typ = (j >> 24) & 0xFF;
j &= 0xFFFFFF;
normalize(&j);
lbuf = j;
if (debug)
printf(" hunk_symbol, typ=%d, len=%d\n",typ,lbuf);
switch(typ)
{
case 0: /* ext_def */
for(i=0; i < lbuf; i++)
{
read(infile,&j,4);
putit(j);
}
read(infile,&j,4); /* value */
putit(j);
break;
}
break;
case 0x3F2: /* hunk_end */
if (debug)
printf("nxtsym= %d\n",nxtsym);
if (nxtsym)
putit(0xF0030000); /* 000003f0 in 8086 order */
for(i = 0; i < nxtsym; i++)
{
/* put out symtable records */
putit(symhunk[i].len);
normalize(&symhunk[i].len);
if (debug)
printf("extsym len = %d\n",symhunk[i].len);
for(j=0; j < symhunk[i].len; j++)
putit(symhunk[i].sym[j]);
putit(symhunk[i].value);
}
if (nxtsym)
putit(0L);
nxtsym = 0;
putit(0xF2030000);
break;
}
}
if (debug)
printf("EOF detected\n");
close(infile);
close(f);
CopyItBack(fname);
}
putit(l)
unsigned long l;
{
write(f,&l,4);
}
normalize(lp)
long *lp;
{
unsigned char hilo[4];
unsigned char lohi[4];
BlockMove(lp,hilo,4);
lohi[0] = hilo[3];
lohi[1] = hilo[2];
lohi[2] = hilo[1];
lohi[3] = hilo[0];
BlockMove(lohi,lp,4);
}
BlockMove(p,q,l)
unsigned char *p,*q;
int l;
{
while(l--)
*q++ = *p++;
}
CopyItBack(fname)
char *fname;
{
int infile;
int f;
char buf[1024]; /* 1k at a time */
long l;
infile = open("cnv.tmp",0);
#ifdef MSC30
f = creat(fname,S_IREAD | S_IWRITE); /* assume binary mode */
#endif
#ifdef LATTI
f = creat(fname, O_RAW | O_RDWR ); /* assume binary mode */
#endif
while(1)
{
l = read(infile,buf,sizeof(buf));
if (l)
write(f,buf,l);
if (l != sizeof(buf))
break;
}
close(infile);
close(f);
unlink("cnv.tmp");
}